package com.crm.services;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.model.jdao.IBackmoneyplanDao;
import com.crm.model.mdao.BackmoneyplanMapper;
import com.crm.model.pojo.Backmoneyplan;
import com.crm.model.pojo.Customer;
import com.crm.model.pojo.Orderinfo;
import com.crm.model.pojo.Returnmoney;
import com.crm.model.pojo.Timeline;
import com.github.pagehelper.PageHelper;

/**
 * 回款业务类
 * @author 周远星
 */
@Service
public class BackmoneyplanService {
	
	@Autowired
	BackmoneyplanMapper bmpMapper;
	
	@Autowired
	IBackmoneyplanDao ibmpDao;
	
	@Autowired
	ReturnMoneyService rmService;
	
	@Autowired
	OrderInfoServers orderServer;

	@Autowired
	CustomerServices cumService;
	
	@Autowired
	TimeLineServices timeLineServices;

	
	/**
	 *  查询回款记录在前台界面展示分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<Map<String, String>> selectByPager(int pageNum, int pageSize,String statusid ,String search){
        //使用分页插件
      System.out.println("pageNum:"+pageNum+",pageSize:"+pageSize);
          //配置分页信息。这句话后面的第一个查询将调用分页。使用分页，对应的Service方法最好只写一个查询方法
      PageHelper.startPage(pageNum, pageSize);
      // 获取
      List<Map<String, String>> list =bmpMapper.findAllBackMoney(statusid,search);
      System.out.println("大小"+list.size());
      return list;
   }
	/**
	 * 添加、修改回款计划
	 */
	public Backmoneyplan addBmp(Backmoneyplan backmoneyplan) {
		if(backmoneyplan.getBmp_isBackMoney().equals("已回")) {
			
			Orderinfo orderinfo = backmoneyplan.getOrderinfo();
			Returnmoney rm=new Returnmoney();
			rm.setCustid(backmoneyplan.getCustomer().getCustId());
			rm.setOrderinfo(orderinfo);
			rm.setRmDate(new Date());
			rm.setRmMoney(backmoneyplan.getBmpMoney());
			rm.setRmStatus(1102);
			rm.setUid(backmoneyplan.getUId());
			
			rmService.addBmp(rm, orderinfo.getOId(), 1+"");
		}
		Backmoneyplan bmp = ibmpDao.save(backmoneyplan);
		System.out.println(bmp);
		return bmp;
	}
	
	/**
	 * 逻辑删除回款计划，将状态改为1103
	 */
	public Backmoneyplan deleteBmp(Integer id) {
		Backmoneyplan bmp = ibmpDao.findById(id).get();
		bmp.setBmpStatus(1103);
		Backmoneyplan save = ibmpDao.save(bmp);
		System.out.println(save);
		return save;
	}
	
	
	/**
	 * 根据id查询
	 */
	public List<Map<String, String>> findById(Integer id) {
		
		return bmpMapper.findById(id);
	}
	
	/**
	 * 根据订单id查询回款计划
	 */
	public List<Map<String, String>> findByoId(Integer id) {
		
		return bmpMapper.findByoId(id);
	}
	 /* 根据客户id查询
	 * @param custid
	 * @return
	 */
	public List<Map<String, String>> findByCustid(Integer custid) {
		return bmpMapper.findByCustid(custid);
	}
	/**
	 * 用于智能添加回款计划
	 * 
	 */
	public void addbmpitemview(List<Map<String, String>> limap) {
		System.out.println("map:"+limap);
		for (Map<String, String> map : limap) {
			Customer customer = cumService.findByCustIdPage(Integer.parseInt(String.valueOf(map.get("comcustid"))));
			Orderinfo orderinfo = orderServer.findById(Integer.parseInt(String.valueOf(map.get("orderid"))));
			Backmoneyplan bmp = new Backmoneyplan();
			bmp.setCustomer(customer);
			bmp.setOrderinfo(orderinfo);
			try {
				bmp.setBmpBmpdate(java.sql.Date.valueOf(String.valueOf(map.get("date1"))));
			}catch (Exception e) {
				bmp.setBmpBmpdate(new Date());
			}
			bmp.setBmpMoney(new BigDecimal(String.valueOf(map.get("bmpmoney"))));
			bmp.setBmp_isBackMoney("未回");
			bmp.setUId(Integer.parseInt(String.valueOf(map.get("uid"))));
			bmp.setBmpForeign(String.valueOf(map.get("textcommunication")));
			bmp.setBmpStatus(1102);
			this.addBmp(bmp);
			Timeline timeline=new Timeline();
			timeline.setTheme("新建回款计划");
			timeline.setTimes(new Date());
			timeline.setCustId(orderinfo.getCustomer().getCustId());
			timeline.setDetails("新建一期回款计划：￥"+bmp.getBmpMoney()+"  元");
			timeLineServices.insertTimeLine(timeline);
			
		}
	}
}
